import java.util.LinkedHashMap;

/**
 * 来自：https://www.modb.pro/db/159334
 *
 * java -Xms10M -Xmx10M -XX:+UseG1GC SystemGCTest 10000
 */
public class SystemGCTest {
    static int Xmx = 10;
    private static final int MB = 1024 * 1024;
    private static byte[] dummy;
    private static Integer[] funny;
    private static LinkedHashMap<Integer, Integer[]> map = new LinkedHashMap<>();
    
    public static void main(String[] args) {
        int loop = Integer.valueOf(args[0]);
        if (loop < 0) {
            loop = loop * -1;
            while (true) {
                doGc(loop);
                map.clear();
                System.gc();
            }
        } else {
            doGc(loop);
            map.clear();
            System.gc();
        }
    }
    
    private static void doGc(int numberOfTimes) {
        final int objectSize = 128;
        final int maxObjectInYoung = (Xmx * MB) / objectSize;
        for (int i = 0; i < numberOfTimes; i++) {
            for (int j = 0; j < maxObjectInYoung + 1; j++) {
                dummy = new byte[objectSize];
                funny = new Integer[objectSize / 16];
                if (j % 10 == 0) {
                    map.put(Integer.valueOf(j), funny);
                }
            }
        }
    }
}
